package org.yar.views;

import org.yar.R;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.SeekBar;
import android.widget.TextView;

public class TextSizeDialog 
	extends AlertDialog
	implements SeekBar.OnSeekBarChangeListener {
	
	public interface OnTextSizeChangedListener {
		void OnTextSizeChanged(TextSizeDialog dialog, int textSize);
	}
	
	public static final int MAX_TEXT_SIZE = 30;
	public static final int MIN_TEXT_SIZE = 12;
	
	private OnTextSizeChangedListener listener;

	private SeekBar sbTextSize;
	private TextView tvTextSize;
	private TextView tvSample;
	
	public TextSizeDialog(Context context, int textSize) {
		super(context);
		
//		setContentView(R.layout.text_size_preference_dialog);
		View myView = LayoutInflater.from(context).inflate(
				R.layout.text_size_preference_dialog, null);
		
		sbTextSize = (SeekBar)myView.findViewById(R.id.sbTextSize);
		sbTextSize.setOnSeekBarChangeListener(this);
		sbTextSize.setMax(MAX_TEXT_SIZE-MIN_TEXT_SIZE);
		sbTextSize.setProgress(textSize-MIN_TEXT_SIZE);
		
		tvTextSize = (TextView)myView.findViewById(R.id.tvTextSize);
		tvTextSize.setText(getCurrentTextSizeString(textSize));
		
		tvSample = (TextView)myView.findViewById(R.id.tvSample);
		tvSample.setTextSize(textSize);
		
		setButton(DialogInterface.BUTTON_POSITIVE, 
				context.getResources().getText(android.R.string.ok), 
				new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				if (listener != null)
				{
					listener.OnTextSizeChanged(TextSizeDialog.this, sbTextSize.getProgress()+MIN_TEXT_SIZE);
				}
				dismiss();
			}
		});
		
		setButton(DialogInterface.BUTTON_NEGATIVE, 
				context.getResources().getText(android.R.string.cancel), 
				new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dismiss();
			}
		});
		
		setView(myView);
	}

	@Override
	public void onProgressChanged(SeekBar seekBar, int progress,
			boolean fromUser) {
		if (fromUser)
		{
			
//			tvTextSize.setTextSize(textSize+MIN_TEXT_SIZE);
			tvTextSize.setText(getCurrentTextSizeString(progress+MIN_TEXT_SIZE));
			tvSample.setTextSize(progress+MIN_TEXT_SIZE);
		}
		
		
	}

	@Override
	public void onStartTrackingTouch(SeekBar seekBar) {
	}

	@Override
	public void onStopTrackingTouch(SeekBar seekBar) {
	}

	public void setOnTextSizedChangedListener(OnTextSizeChangedListener listener) {
		this.listener = listener;
	}
	
	private String getCurrentTextSizeString(int textSize)
	{
		String curr = getContext().getResources().getString(R.string.text_size_current);
		return curr + " " + textSize;
	}
}
